package com.example.rouroudate.ui.widget

import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.example.rouroudate.R
import com.example.rouroudate.util.DisplayUtils
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog

open class CommonBottomDialog constructor(context: Context) : BottomSheetDialog(context, R.style.BottomSheetDialog) {

    private var container: LinearLayout

    private var content: View

    private var titleBar: FrameLayout

    private lateinit var commonBehavior: BottomSheetBehavior<View>

    init {
        content = View.inflate(context, R.layout.common_bottom_layout, null)
        super.setContentView(content)
        titleBar = content.findViewById(R.id.bottom_title_bar)
        container = content.findViewById<LinearLayout?>(R.id.bottom_container).apply {
            val lp = layoutParams
            lp.height = (DisplayUtils.getScreenHeight() * 0.3f).toInt()
        }
        initBG()
        initBehavior()
        initTitleBar()
    }

    open fun setTitle(title: String) {
        titleBar.findViewById<TextView>(R.id.title_tv).apply {
            text = title
            setTextColor(ContextCompat.getColor(context, R.color.pink))
        }
    }

    protected fun setRightViewClick(listener: View.OnClickListener) {
        titleBar.findViewById<TextView>(R.id.right_view).apply {
            setOnClickListener(listener)
        }
    }

    private fun initBG() {
        val bg = GradientDrawable()
        val radius = DisplayUtils.dp2px(8f).toFloat()
        bg.cornerRadii = floatArrayOf(radius, radius, radius, radius, 0f, 0f, 0f, 0f)
        bg.setColor(ContextCompat.getColor(context,R.color.white))
        bg.shape = GradientDrawable.RECTANGLE
        content.background = bg
    }

    private fun initTitleBar() {
        titleBar.apply {
            findViewById<TextView>(R.id.left_view).apply {
                text = context.getString(R.string.cancel)
                textSize = DisplayUtils.sp2px(6f).toFloat()
                setOnClickListener { dismiss() }
            }
            findViewById<TextView>(R.id.right_view).apply {
                text = context.getString(R.string.confirm)
                setTextColor(ContextCompat.getColor(context, R.color.pink))
                textSize = DisplayUtils.sp2px(6f).toFloat()
            }
        }
    }

    private fun initBehavior() {
        val viewParent = content.parent
        if (viewParent is View) {
            commonBehavior = BottomSheetBehavior.from(viewParent)
            commonBehavior.state = BottomSheetBehavior.STATE_EXPANDED
            commonBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
                override fun onStateChanged(bottomSheet: View, newState: Int) {
                    if (newState == BottomSheetBehavior.STATE_DRAGGING) {
                        behavior.state = BottomSheetBehavior.STATE_EXPANDED
                    }
                }

                override fun onSlide(bottomSheet: View, slideOffset: Float) {
                }

            })
        }
    }

    override fun setContentView(layoutResId: Int) {
        View.inflate(context, layoutResId, container)
    }
}